package com.ymatou.iapi.trading.userquery.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.parameter.mongo.UserIndex;
import com.ymatou.iapi.trading.userquery.parameter.GetOrderCountByTimeRequest;
import com.ymatou.iapi.trading.userquery.service.GetOrderCountByTimeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.mongo.UserIndexQueryWapper;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetOrderCountByTime {
	
	private static GetOrderCountByTimeRequest getOrderCountByTimeRequest;
	private static GetOrderCountByTimeCall getOrderCountByTimeCall;
	
	private static Integer userId = 20238699;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取某个时间点后的用户下单数");
	}

	@Before
	public void caseUp() {
		getOrderCountByTimeRequest = new GetOrderCountByTimeRequest();
		getOrderCountByTimeCall = new GetOrderCountByTimeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getOrderCountByTimeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify() throws JSONException {
		
		int expectCount = getOrderCountByTimeCall.getData().getInt("count");
		boolean expectSuccess = getOrderCountByTimeCall.getData().getBoolean("success");
		
		List<UserIndex> orderList = new UserIndexQueryWapper()
				.findByUserIdAndSellerId(getOrderCountByTimeRequest.getBuyerId(),
						getOrderCountByTimeRequest.getSellerId());
		
		int actualCount = orderList
				.stream()
				.filter(UserIndex -> UserIndex.getAddTime() != null)
				.filter(UserIndex -> UserIndex.getAddTime().getTime() > YMTDateUtil
						.getDate(getOrderCountByTimeRequest.getTime(),
								YMTDateUtil.YMDTHMS_STRING).getTime())
				.collect(Collectors.toList()).size();
		
		Logger.verifyEquals(expectCount, actualCount, "订单数量验证");
		Logger.verifyEquals(true, expectSuccess, "接口是否正确返回");
		
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCountByTime_001() {
		Logger.start(true, "获取用户30分钟内的下单数");
		try {
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long userId = placeOrder.getUserId();
			String time = YMTDateUtil.getBeforeOrNextMinutes(
					YMTDateUtil.getSysTime(YMTDateUtil.YMDTHMS_STRING), 30,
					YMTDateUtil.YMDTHMS_STRING);
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");
			
			getOrderCountByTimeRequest.setBuyerId(userId);
			getOrderCountByTimeRequest.setSellerId(sellerId);
			getOrderCountByTimeRequest.setTime(time);

			getOrderCountByTimeCall.setData(getOrderCountByTimeRequest);
			getOrderCountByTimeCall.callService();
			
			Logger.verifyEquals(200, getOrderCountByTimeCall.getCode(), "校验code");
			Logger.verifyEquals(0, getOrderCountByTimeCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCountByTime_002() {
		Logger.start(true, "获取用户30分钟内的下单数, 订单被取消");
		try {
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long userId = placeOrder.getUserId();
			String time = YMTDateUtil.getBeforeOrNextMinutes(
					YMTDateUtil.getSysTime(YMTDateUtil.YMDTHMS_STRING), 30,
					YMTDateUtil.YMDTHMS_STRING);
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			
			TradingCallService.cancelOrder(orderId, CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);

			Logger.comment("前置数据准备结束");
			
			getOrderCountByTimeRequest.setBuyerId(userId);
			getOrderCountByTimeRequest.setSellerId(sellerId);
			getOrderCountByTimeRequest.setTime(time);

			getOrderCountByTimeCall.setData(getOrderCountByTimeRequest);
			getOrderCountByTimeCall.callService();
			
			Logger.verifyEquals(200, getOrderCountByTimeCall.getCode(), "校验code");
			Logger.verifyEquals(0, getOrderCountByTimeCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCountByTime_003() {
		Logger.start(true, "获取用户30分钟内的下单数, 用户未下单");
		try {
			Logger.comment("前置数据准备");
			
			String time = YMTDateUtil.getBeforeOrNextMinutes(
					YMTDateUtil.getSysTime(YMTDateUtil.YMDTHMS_STRING), 30,
					YMTDateUtil.YMDTHMS_STRING);

			Logger.comment("前置数据准备结束");
			
			getOrderCountByTimeRequest.setBuyerId(30252346);
			getOrderCountByTimeRequest.setSellerId(24635724);
			getOrderCountByTimeRequest.setTime(time);

			getOrderCountByTimeCall.setData(getOrderCountByTimeRequest);
			getOrderCountByTimeCall.callService();
			
			Logger.verifyEquals(200, getOrderCountByTimeCall.getCode(), "校验code");
			Logger.verifyEquals(0, getOrderCountByTimeCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCountByTime_004() {
		Logger.start(true, "获取新用户5分钟内的下单数, 用户已下单");
		try {
			Logger.comment("前置数据准备");

			int userId = new OrderCallService().createNewUser();
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithUserId(userId);
			String time = YMTDateUtil.getBeforeOrNextMinutes(
					YMTDateUtil.getSysTime(YMTDateUtil.YMDTHMS_STRING), 5,
					YMTDateUtil.YMDTHMS_STRING);
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");
			
			getOrderCountByTimeRequest.setBuyerId(userId);
			getOrderCountByTimeRequest.setSellerId(sellerId);
			getOrderCountByTimeRequest.setTime(time);

			getOrderCountByTimeCall.setData(getOrderCountByTimeRequest);
			getOrderCountByTimeCall.callService();
			
			Logger.verifyEquals(200, getOrderCountByTimeCall.getCode(), "校验code");
			Logger.verifyEquals(0, getOrderCountByTimeCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCountByTime_005() {
		Logger.start(true, "获取新用户5分钟内的下单数, 用户未下单");
		try {
			Logger.comment("前置数据准备");
			
			int userId = new OrderCallService().createNewUser();
			String time = YMTDateUtil.getBeforeOrNextMinutes(
					YMTDateUtil.getSysTime(YMTDateUtil.YMDTHMS_STRING), 5,
					YMTDateUtil.YMDTHMS_STRING);

			Logger.comment("前置数据准备结束");
			
			getOrderCountByTimeRequest.setBuyerId(userId);
			getOrderCountByTimeRequest.setSellerId(24635724);
			getOrderCountByTimeRequest.setTime(time);

			getOrderCountByTimeCall.setData(getOrderCountByTimeRequest);
			getOrderCountByTimeCall.callService();
			
			Logger.verifyEquals(200, getOrderCountByTimeCall.getCode(), "校验code");
			Logger.verifyEquals(0, getOrderCountByTimeCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCountByTime_006() {
		Logger.start(true, "userId为0");
		try {
			
			String time = YMTDateUtil.getBeforeOrNextMinutes(
					YMTDateUtil.getSysTime(YMTDateUtil.YMDTHMS_STRING), 5,
					YMTDateUtil.YMDTHMS_STRING);
			
			getOrderCountByTimeRequest.setBuyerId(0);
			getOrderCountByTimeRequest.setSellerId(24635724);
			getOrderCountByTimeRequest.setTime(time);

			getOrderCountByTimeCall.setData(getOrderCountByTimeRequest);
			getOrderCountByTimeCall.callService();
			
			Logger.verifyEquals(400, getOrderCountByTimeCall.getCode(), "校验code");
			Logger.verifyEquals(100, getOrderCountByTimeCall.getBCode(), "校验bcode");
			Logger.verifyEquals("buyerId:buyerId未提供",
					getOrderCountByTimeCall.getMsg(), "校验msg");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", getOrderCountByTimeCall
					.getData().getString("errorCode"), "校验errorCode");
			Logger.verifyEquals("buyerId:buyerId未提供", getOrderCountByTimeCall
					.getData().getString("errorMessage"), "校验errorMessage");
			Logger.verifyEquals(false, getOrderCountByTimeCall.getData()
					.getBoolean("success"), "校验success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCountByTime_007() {
		Logger.start(true, "sellerId为0");
		try {
			
			String time = YMTDateUtil.getBeforeOrNextMinutes(
					YMTDateUtil.getSysTime(YMTDateUtil.YMDTHMS_STRING), 5,
					YMTDateUtil.YMDTHMS_STRING);
			
			getOrderCountByTimeRequest.setBuyerId(24787767);
			getOrderCountByTimeRequest.setSellerId(0);
			getOrderCountByTimeRequest.setTime(time);

			getOrderCountByTimeCall.setData(getOrderCountByTimeRequest);
			getOrderCountByTimeCall.callService();
			
			Logger.verifyEquals(400, getOrderCountByTimeCall.getCode(), "校验code");
			Logger.verifyEquals(100, getOrderCountByTimeCall.getBCode(), "校验bcode");
			Logger.verifyEquals("sellerId:sellerId未提供",
					getOrderCountByTimeCall.getMsg(), "校验msg");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", getOrderCountByTimeCall
					.getData().getString("errorCode"), "校验errorCode");
			Logger.verifyEquals("sellerId:sellerId未提供", getOrderCountByTimeCall
					.getData().getString("errorMessage"), "校验errorMessage");
			Logger.verifyEquals(false, getOrderCountByTimeCall.getData()
					.getBoolean("success"), "校验success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCountByTime_008() {
		Logger.start(true, "time为空");
		try {
			
			getOrderCountByTimeRequest.setBuyerId(24787767);
			getOrderCountByTimeRequest.setSellerId(24635724);
			getOrderCountByTimeRequest.setTime("");

			getOrderCountByTimeCall.setData(getOrderCountByTimeRequest);
			getOrderCountByTimeCall.callService();
			
			Logger.verifyEquals(400, getOrderCountByTimeCall.getCode(), "校验code");
			Logger.verifyEquals(100, getOrderCountByTimeCall.getBCode(), "校验bcode");
			Logger.verifyEquals("time:may not be null",
					getOrderCountByTimeCall.getMsg(), "校验msg");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", getOrderCountByTimeCall
					.getData().getString("errorCode"), "校验errorCode");
			Logger.verifyEquals("time:may not be null", getOrderCountByTimeCall
					.getData().getString("errorMessage"), "校验errorMessage");
			Logger.verifyEquals(false, getOrderCountByTimeCall.getData()
					.getBoolean("success"), "校验success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
